package com.htx.zqs.blesmartmask.bean;

import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.htx.zqs.blesmartmask.MyApplication;
import com.htx.zqs.blesmartmask.db.ManagerFactory;
import com.htx.zqs.blesmartmask.utils.LocationUtil;
import com.htx.zqs.blesmartmask.utils.MySpUtils;
import com.htx.zqs.blesmartmask.utils.Utils;
import com.htx.zqs.greendao.gen.AfterCourseDataDao;
import com.htx.zqs.greendao.gen.BeforeCourseDataDao;

import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Generated;

import java.util.List;

@Entity
public class AfterCourseData implements Comparable<AfterCourseData> {
    @Id(autoincrement = true)
    private Long sId;
    private int id;
    private String islogo;
    private String coursedate;
    private String name;
    private String deviceid;
    private int afterffat;
    private int afterfwater;
    private String afterdate;
    private String voltage;
    private String time;
    private String address;
    private String isUpNet; //是否这条数据上传网络

    public AfterCourseData(String afterdate, int afterfwater, int afterffat) {
        this.afterdate = afterdate;
        this.afterfwater = afterfwater;
        this.afterffat = afterffat;
        this.islogo = "A";
        this.name = MyApplication.selectCourse.getName();
        this.coursedate = MyApplication.selectCourse.getCoursedate();
        this.deviceid = LocationUtil.getUniquePsuedoID();
        this.voltage = MySpUtils.getString(MySpUtils.KEY_COURSE_VOLTAGE);
        this.time = MySpUtils.getString(MySpUtils.KEY_COURSE_TIME);
        this.isUpNet = "0";
        this.address = Utils.getCurMac();
    }


    @Generated(hash = 1505719677)
    public AfterCourseData() {
    }


    @Generated(hash = 2142780605)
    public AfterCourseData(Long sId, int id, String islogo, String coursedate, String name, String deviceid,
            int afterffat, int afterfwater, String afterdate, String voltage, String time, String address,
            String isUpNet) {
        this.sId = sId;
        this.id = id;
        this.islogo = islogo;
        this.coursedate = coursedate;
        this.name = name;
        this.deviceid = deviceid;
        this.afterffat = afterffat;
        this.afterfwater = afterfwater;
        this.afterdate = afterdate;
        this.voltage = voltage;
        this.time = time;
        this.address = address;
        this.isUpNet = isUpNet;
    }


    public String getTime() {
        return time == null ? "" : time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getVoltage() {
        return voltage == null ? "" : voltage;
    }

    public void setVoltage(String voltage) {
        this.voltage = voltage;
    }

    public Long getSId() {
        return this.sId;
    }

    public void setSId(Long sId) {
        this.sId = sId;
    }

    public String getIslogo() {
        return this.islogo;
    }

    public void setIslogo(String islogo) {
        this.islogo = islogo;
    }

    public String getCoursedate() {
        return this.coursedate;
    }

    public void setCoursedate(String coursedate) {
        this.coursedate = coursedate;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDeviceid() {
        return this.deviceid;
    }

    public void setDeviceid(String deviceid) {
        this.deviceid = deviceid;
    }

    public int getAfterffat() {
        return this.afterffat;
    }

    public void setAfterffat(int afterffat) {
        this.afterffat = afterffat;
    }

    public int getAfterfwater() {
        return this.afterfwater;
    }

    public void setAfterfwater(int afterfwater) {
        this.afterfwater = afterfwater;
    }

    public String getAfterdate() {
        return this.afterdate;
    }

    public void setAfterdate(String afterdate) {
        this.afterdate = afterdate;
    }

    public String getIsUpNet() {
        return this.isUpNet;
    }

    public void setIsUpNet(String isUpNet) {
        this.isUpNet = isUpNet;
    }

    //获取离线数据 油水测试除外
    public static List<AfterCourseData> findOfflineData(String courseDate) {
        return ManagerFactory.getInstance()
                .getAfterCourseManager()
                .queryBuilder()
                .where(AfterCourseDataDao.Properties.Coursedate.eq(courseDate))
                .list();
    }

    public static List<AfterCourseData> findAll() {
        return ManagerFactory.getInstance().getAfterCourseManager()
                .queryAll();
    }

    public static List<AfterCourseData> findAllNoUp() {
        return ManagerFactory.getInstance()
                .getAfterCourseManager()
                .queryBuilder()
                .where(AfterCourseDataDao.Properties.IsUpNet.eq("0"))
                .list();
    }

    public static void updateUpState() {
        List<AfterCourseData> list = findAllNoUp();
        for (AfterCourseData afterCourseData : list) {
            afterCourseData.setIsUpNet("1");
        }
        ManagerFactory.getInstance().getAfterCourseManager().update(list);
    }

    public static void save(List<AfterCourseData> list) {
        ManagerFactory.getInstance().getAfterCourseManager().save(list);
    }

    public static void save(AfterCourseData afterCourseData) {
        for (AfterCourseData data : findAll()) {
            if (TextUtils.equals(data.getAfterdate(), afterCourseData.getAfterdate()) &&
                    TextUtils.equals(data.getCoursedate(), afterCourseData.getCoursedate()))
                return;
        }
        ManagerFactory.getInstance().getAfterCourseManager().save(afterCourseData);
    }


    public String getAddress() {
        return this.address;
    }


    public void setAddress(String address) {
        this.address = address;
    }


    public int getId() {
        return this.id;
    }


    public void setId(int id) {
        this.id = id;
    }
    @Override
    public int compareTo(@NonNull AfterCourseData o) {
        return o.id - id;
    }
}
